package com.pink.controller;

import com.pink.component.RedisComponent;
import com.pink.entity.dto.UserDto;
import com.pink.entity.enums.ResponseCodeEnum;
import com.pink.entity.query.EmailQuery;
import com.pink.entity.vo.ResponseVo;
import com.pink.entity.vo.RestBeanVo;
import com.pink.entity.vo.response.UserInformVo;
import com.pink.service.UserService;
import com.pink.utils.CodeUtils;
import com.pink.utils.StringTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.*;

@RestController
@RequestMapping("/api/account")
@Validated
public class AccountController {

    @Autowired
    private UserService userService;

    @Resource
    RedisComponent redisComponent;

    @Resource
    CodeUtils captcha;

    /**
     * 登录验证码获取
     * @return
     */
    @RequestMapping("/checkCode")
    public ResponseVo checkCode() {
        return ResponseVo.success(captcha.getCaptcha());
    }

    /**
     * 邮箱验证码获取
     * @param email
     * @return
     */
    @RequestMapping("/askCode")
    public ResponseVo askCode(@NotEmpty @Email @Size(max = 150) String email) {
        EmailQuery emailCode  = redisComponent.getEmailQueue(email);
        if(emailCode != null){
            return ResponseVo.error("请勿频繁发送验证码！");
        }
        String code = StringTool.generateRandomNumber();
        EmailQuery emailQuery = new EmailQuery();
        emailQuery.setEmail(email);
        emailQuery.setCode(Integer.parseInt(code));
        emailQuery.setType("register");
        redisComponent.setEmailQueue(emailQuery);
        return ResponseVo.success("发送成功！");
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public ResponseVo register(UserDto user,String code) {
        if(code == null){
            return ResponseVo.error(ResponseCodeEnum.CODE_600.getMessage());
        }
        EmailQuery emailQuery = redisComponent.getEmailQueue(user.getEmail());
        if(emailQuery == null||emailQuery.getCode() != Integer.parseInt(code)){
            return ResponseVo.error(ResponseCodeEnum.CODE_EMAIL_CODE.getMessage());
        }
        redisComponent.delEmailQueue(user.getEmail());
        return ResponseVo.success(userService.register(user));
    }

    @RequestMapping("/getUserInform")
    public ResponseVo<UserInformVo> sendEmail(String username) {
        UserInformVo user = userService.findAccountByNameOrUsername(username);
        return ResponseVo.success("查询成功！",user);
    }

    //验证码验证
    @RequestMapping("/codeCheckOk")
    public ResponseVo sendEmail(String code,String cokeKey) {
        if (code == null || code.isEmpty()|| cokeKey == null || cokeKey.isEmpty()) {
            return ResponseVo.error(ResponseCodeEnum.CODE_600.getMessage());
        }
        String dbcode = redisComponent.getChckeCode(cokeKey);
        if (dbcode == null || !dbcode.equals(code)) {
            return ResponseVo.error(ResponseCodeEnum.CODE_600.getMessage());
        }
        return ResponseVo.success("验证成功！");
    }

}
